package com.weishenai.customerassistant.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishenai.customerassistant.entity.PublishingTask;
import com.weishenai.customerassistant.mapper.PublishingTaskMapper;
import com.weishenai.customerassistant.service.PublishingTaskService;
import com.weishenai.customerassistant.utils.DateUtil;
import com.weishenai.customerassistant.utils.UniqueIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class PublishingTaskServiceImpl extends ServiceImpl<PublishingTaskMapper, PublishingTask> implements PublishingTaskService {

    @Autowired
    private PublishingTaskMapper publishingTaskMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public IPage<PublishingTask> selectBySubstation(String substation, Integer pageNum, Integer pageSize, String keyWord) {
        if ("null".equals(keyWord)) {
            keyWord = null;
        }
        return publishingTaskMapper.selectPage(new Page<>(pageNum, pageSize),
                Wrappers.lambdaQuery(PublishingTask.class)
                        .eq(PublishingTask::getSubstation, substation)
                        .eq(StrUtil.isNotBlank(keyWord), PublishingTask::getFactName, keyWord));
    }

    @Override
    public Integer addOrUpdateTask(PublishingTask publishingTask) {
        if (publishingTask.getId() == null || publishingTask.getId() == 0) {
            publishingTask.setId(Long.parseLong(UniqueIdUtil.getResultId(redisTemplate, false)));
            publishingTask.setState("未勘测");
            publishingTask.setCreatedTime(DateUtil.getTime());
            return publishingTaskMapper.insert(publishingTask);
        }
        return publishingTaskMapper.updateById(publishingTask);
    }

    @Override
    public int deleteById(Long id) {
        return publishingTaskMapper.deleteById(id);
    }

    /**
     * 小程序查询用户的任务
     *
     * @param wechatId 微信id
     * @param pageNum  分页，当前页
     * @param pageSize 分页，每页的大小
     * @return
     */
    @Override
    public IPage<PublishingTask> selectByWechatId(String wechatId, Integer pageNum, Integer pageSize, String state) {
        return publishingTaskMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.lambdaQuery(PublishingTask.class)
                .eq(PublishingTask::getWxId, wechatId).
                eq(PublishingTask::getState, state)
        );
    }

    @Override
    public int updateState(String wechatId, String state, Long id) {
        return publishingTaskMapper.updateStateByWechatId(wechatId, state, id);
    }

    @Override
    public int deleteByState(String state) {
        return publishingTaskMapper.delete(Wrappers.lambdaQuery(PublishingTask.class)
                .eq(PublishingTask::getState, state));
    }
}
